#!/bin/bash

echo "=========================================="
echo "Completion 字段 input 数组设计演示"
echo "=========================================="

ES_URL="http://117.72.38.248:19200"

# 创建设计演示索引
echo "1. 创建设计演示索引..."
curl -s -X DELETE "$ES_URL/input-design-demo" > /dev/null

curl -s -X PUT "$ES_URL/input-design-demo" -H "Content-Type: application/json" -d '{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "nameSuggest": {
        "type": "completion",
        "analyzer": "simple",
        "search_analyzer": "simple"
      }
    }
  }
}' > /dev/null

echo "✅ 设计演示索引创建完成"

# 插入不同设计策略的数据
echo ""
echo "2. 插入不同设计策略的数据..."

# 策略1：基础设计 - 只包含原始数据
curl -s -X POST "$ES_URL/input-design-demo/_doc/1" -H "Content-Type: application/json" -d '{
  "name": "iPhone 15 Pro Max",
  "nameSuggest": {
    "input": ["iPhone 15 Pro Max"],
    "weight": 10
  }
}' > /dev/null

# 策略2：扩展设计 - 包含变体和缩写
curl -s -X POST "$ES_URL/input-design-demo/_doc/2" -H "Content-Type: application/json" -d '{
  "name": "MacBook Pro 16-inch",
  "nameSuggest": {
    "input": [
      "MacBook Pro 16-inch",
      "MacBook Pro 16",
      "MacBook Pro",
      "MacBook",
      "MBP 16",
      "MBP",
      "苹果笔记本",
      "苹果电脑"
    ],
    "weight": 9
  }
}' > /dev/null

# 策略3：多语言设计 - 支持中英文
curl -s -X POST "$ES_URL/input-design-demo/_doc/3" -H "Content-Type: application/json" -d '{
  "name": "AirPods Pro",
  "nameSuggest": {
    "input": [
      "AirPods Pro",
      "AirPods",
      "苹果耳机",
      "无线耳机",
      "降噪耳机",
      "APP",
      "airpods pro",
      "airpods"
    ],
    "weight": 8
  }
}' > /dev/null

# 策略4：用户行为驱动设计 - 基于搜索热词
curl -s -X POST "$ES_URL/input-design-demo/_doc/4" -H "Content-Type: application/json" -d '{
  "name": "iPad Air 5th Generation",
  "nameSuggest": {
    "input": [
      "iPad Air 5th Generation",
      "iPad Air 5",
      "iPad Air",
      "iPad",
      "平板电脑",
      "苹果平板",
      "air5",
      "ipad air 5",
      "新iPad",
      "2022 iPad"
    ],
    "weight": 7
  }
}' > /dev/null

# 策略5：智能设计 - 包含拼音和常见错误
curl -s -X POST "$ES_URL/input-design-demo/_doc/5" -H "Content-Type: application/json" -d '{
  "name": "Apple Watch Series 9",
  "nameSuggest": {
    "input": [
      "Apple Watch Series 9",
      "Apple Watch Series",
      "Apple Watch",
      "Watch Series 9",
      "Watch 9",
      "苹果手表",
      "智能手表",
      "apple watch",
      "watch series 9",
      "aw9",
      "apple watch 9",
      "苹果表"
    ],
    "weight": 6
  }
}' > /dev/null

echo "✅ 不同设计策略的数据插入完成"

# 等待索引刷新
sleep 2

# 测试不同设计策略的效果
echo ""
echo "3. 测试不同设计策略的效果..."

echo ""
echo "🔍 测试1：基础设计 - 只包含原始数据"
echo "输入 'iPhone' 应该能匹配到："
curl -s "$ES_URL/input-design-demo/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "test_suggest": {
      "prefix": "iPhone",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq -r '.suggest.test_suggest[0].options[] | "  ✅ " + .text + " (来源: " + ._source.name + ")"'

echo ""
echo "🔍 测试2：扩展设计 - 包含变体和缩写"
echo "输入 'MBP' 应该能匹配到："
curl -s "$ES_URL/input-design-demo/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "test_suggest": {
      "prefix": "MBP",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq -r '.suggest.test_suggest[0].options[] | "  ✅ " + .text + " (来源: " + ._source.name + ")"'

echo ""
echo "🔍 测试3：多语言设计 - 支持中英文"
echo "输入 '苹果' 应该能匹配到："
curl -s "$ES_URL/input-design-demo/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "test_suggest": {
      "prefix": "苹果",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq -r '.suggest.test_suggest[0].options[] | "  ✅ " + .text + " (来源: " + ._source.name + ")"'

echo ""
echo "🔍 测试4：用户行为驱动设计 - 基于搜索热词"
echo "输入 '新iPad' 应该能匹配到："
curl -s "$ES_URL/input-design-demo/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "test_suggest": {
      "prefix": "新iPad",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq -r '.suggest.test_suggest[0].options[] | "  ✅ " + .text + " (来源: " + ._source.name + ")"'

echo ""
echo "🔍 测试5：智能设计 - 包含拼音和常见错误"
echo "输入 'apple' 应该能匹配到："
curl -s "$ES_URL/input-design-demo/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "test_suggest": {
      "prefix": "apple",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq -r '.suggest.test_suggest[0].options[] | "  ✅ " + .text + " (来源: " + ._source.name + ")"'

echo ""
echo "=========================================="
echo "input 数组设计策略总结"
echo "=========================================="
echo ""
echo "📋 设计策略对比："
echo ""
echo "1️⃣ 基础设计："
echo "   ✅ 简单直接"
echo "   ❌ 匹配率低"
echo "   📝 适用：简单场景"
echo ""
echo "2️⃣ 扩展设计："
echo "   ✅ 包含变体和缩写"
echo "   ✅ 提高匹配率"
echo "   📝 适用：产品名称"
echo ""
echo "3️⃣ 多语言设计："
echo "   ✅ 支持中英文"
echo "   ✅ 国际化友好"
echo "   📝 适用：多语言应用"
echo ""
echo "4️⃣ 用户行为驱动："
echo "   ✅ 基于真实搜索数据"
echo "   ✅ 用户体验好"
echo "   📝 适用：电商平台"
echo ""
echo "5️⃣ 智能设计："
echo "   ✅ 包含拼音和常见错误"
echo "   ✅ 容错性强"
echo "   📝 适用：复杂业务场景"
echo ""
echo "🎯 最佳实践："
echo "1. 结合多种策略"
echo "2. 基于用户行为数据"
echo "3. 定期优化和更新"
echo "4. 考虑业务特点"
echo "5. 测试和验证效果"
